<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <link rel="stylesheet" href="styles/style.css">
        <link rel="stylesheet" href="styles/theme/atom-one-dark.css">
        <title>小练习4-JS进阶（二）</title>
    </head>
    <body class="cols">
        <pre>
            <code>
                    // 小练习4：以下数组的方法中哪些是纯函数，哪些不是纯函数？

                    slice、splice、pop、push、shift、unshift、reverse、concat、join
            </code>
        </pre>
        <pre>
            <code>
                    // 解法：
                    var a1 = [1, 2, 3, 4, 5];
                    var a2 = [1, 2, 3, 4, 5];
                    var a3 = [1, 2, 3, 4, 5];
                    var a4 = [1, 2, 3, 4, 5];
                    var a5 = [1, 2, 3, 4, 5];
                    var a6 = [1, 2, 3, 4, 5];
                    var a7 = [1, 2, 3, 4, 5];
                    var a8 = [1, 2, 3, 4, 5];
                    var a9 = [1, 2, 3, 4, 5];

                    console.log('a1: [' + a1 + ']');
                    console.log('a1.slice(1, 3) => [' + a1.slice(1, 3) + '] // 源数组不变, 纯函数');
                    console.log('a1: [' + a1 + ']');

                    console.log('------------------------------------------------');

                    console.log('a2: [' + a2 + ']');
                    console.log('a2.splice(1, 3) => [' + a2.splice(1, 3) + '] // 源数组改变, 不是纯函数');
                    console.log('a2: [' + a2 + ']');

                    console.log('------------------------------------------------');

                    console.log('a3: [' + a3 + ']');
                    console.log('a3.pop() => ' + a3.pop() + ' // 源数组改变, 不是纯函数');
                    console.log('a3: [' + a3 + ']');

                    console.log('------------------------------------------------');

                    console.log('a4: [' + a4 + ']');
                    console.log('a4.push(6) => ' + a4.push(6) + ' // 源数组改变, 不是纯函数');
                    console.log('a4: [' + a4 + ']');

                    console.log('------------------------------------------------');

                    console.log('a5: [' + a5 + ']');
                    console.log('a5.shift() => ' + a5.shift() + ' // 源数组改变, 不是纯函数');
                    console.log('a5: [' + a5 + ']');

                    console.log('------------------------------------------------');

                    console.log('a6: [' + a6 + ']');
                    console.log('a6.unshift(1) => ' + a6.unshift(1) + ' // 源数组改变, 不是纯函数');
                    console.log('a6: [' + a6 + ']');

                    console.log('------------------------------------------------');

                    console.log('a7: [' + a7 + ']');
                    console.log('a7.reverse() => [' + a7.reverse() + '] // 源数组改变, 不是纯函数');
                    console.log('a7: [' + a7 + ']');

                    console.log('------------------------------------------------');

                    console.log('a8: [' + a8 + ']');
                    console.log('a8.concat([6, 7]) => [' + a8.concat([6, 7]) + '] // 源数组不变, 纯函数');
                    console.log('a8: [' + a8 + ']');

                    console.log('------------------------------------------------');

                    console.log('a9: [' + a9 + ']');
                    console.log('a9.join("-") => "' + a9.join('-') + '" // 源数组不变, 纯函数');
                    console.log('a9: [' + a9 + ']');
            </code>
        </pre>
        <script src="scripts/libs/highlight.pack.js"></script>
        <script src="scripts/p_4.js"></script>
        <script>
            hljs.initHighlightingOnLoad();
        </script>
    </body>
</html>